// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.editor;

import org.dengues.core.CorePlugin;
import org.dengues.core.components.IComponentsFactory;
import org.dengues.core.editor.utils.GEFEditorUtils;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.core.warehouse.IWarehouseView;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.KeyHandler;
import org.eclipse.gef.KeyStroke;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.ui.palette.PaletteViewerProvider;
import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;

public abstract class AbstractGenericGEFEditor extends GraphicalEditorWithFlyoutPalette implements
        ITabbedPropertySheetPageContributor {

    private static Point mouseLocation = new Point();

    protected final IComponentsFactory factory = CorePlugin.getDefault().getDesignerCoreService().getComponentsFactory();

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#getGraphicalControl()
     */
    @Override
    public Control getGraphicalControl() {
        Control graphicalControl = super.getGraphicalControl();
        graphicalControl.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                mouseLocation.x = e.x;
                mouseLocation.y = e.y;
            }

        });
        return graphicalControl;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getMouseLocation".
     * 
     * @return
     */
    public static Point getMouseLocation() {
        return mouseLocation;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCommonKeyHandler".
     * 
     * @return
     */
    protected KeyHandler getCommonKeyHandler() {
        KeyHandler sharedKeyHandler = new KeyHandler();
        sharedKeyHandler.put(KeyStroke.getPressed(SWT.DEL, 0), getActionRegistry().getAction(ActionFactory.DELETE.getId()));
        return sharedKeyHandler;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
     */
    @Override
    protected void setInput(IEditorInput input) {
        super.setInput(input);
        if (input instanceof AbstractEditorInput) {
            AbstractEditorInput processEditorInput = ((AbstractEditorInput) input);
            processEditorInput.getWarehouseNode().setNodeStatus(ENodeStatus.ACTIVED);
            refreshWarehouseView();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditor#selectionChanged(org.eclipse.ui.IWorkbenchPart,
     * org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(final IWorkbenchPart part, final ISelection selection) {
        super.selectionChanged(part, selection);
        if (this.equals(part)) { // Propagated from MyMultiPageEditor.
            updateActions(getSelectionActions());
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditor#dispose()
     */
    @Override
    public void dispose() {
        if (getEditorInput() instanceof AbstractEditorInput) {
            AbstractEditorInput processEditorInput = ((AbstractEditorInput) getEditorInput());
            processEditorInput.getWarehouseNode().setNodeStatus(ENodeStatus.NORMAL);
            refreshWarehouseView();
        }
        super.dispose();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractGenericGEFEditor constructor comment.
     */
    public AbstractGenericGEFEditor() {
        setEditDomain(new DefaultEditDomain(this));
        GEFEditorUtils.registActiveEditor(this);
    }

    public GraphicalViewer getViewer() {
        return getGraphicalViewer();
    }

    private boolean dirtyState = false;

    @Override
    public boolean isDirty() {
        // rely on the command stack to determine dirty flag
        return dirtyState || getCommandStack().isDirty();
    }

    public void setDirty(boolean dirty) {
        dirtyState = dirty;
        if (dirtyState) {
            firePropertyChange(IEditorPart.PROP_DIRTY);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#getPalettePreferences()
     */
    @Override
    protected FlyoutPreferences getPalettePreferences() {
        return GEFEditorUtils.createPalettePreferences();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditor#getCommandStack()
     */
    @Override
    public CommandStack getCommandStack() {
        return super.getCommandStack();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getAction".
     * 
     * @param actionID
     * @return
     */
    public IAction getAction(String actionID) {
        return getActionRegistry().getAction(actionID);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#createPaletteViewerProvider()
     */
    @Override
    protected PaletteViewerProvider createPaletteViewerProvider() {
        return GEFEditorUtils.createPaletteViewerProvider(getEditDomain());
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshWarehouseView".
     */
    protected void refreshWarehouseView() {
        IViewPart findView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                .findView(IWarehouseView.VIEW_ID);
        if (findView instanceof IWarehouseView) {
            ((IWarehouseView) findView).refresh(((AbstractEditorInput) getEditorInput()).getWarehouseNode().getParent());
        }
    }
}
